package main.main;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.WindowManager;

public class Ball {
	private Bitmap myImage;
	private int myX, myY, myId, myXV, myYV, myRadius;
	public static int BallCount=1;
	private static int Y_MAX=724, X_MAX=270;
	//private int myDirection, mySpeed;
	public Ball(Context context, int x, int y, int radius) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		myId = BallCount;
		BallCount++;
		WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		//Y_MAX=manager.getDefaultDisplay().getHeight();
		X_MAX=manager.getDefaultDisplay().getWidth();
		myX=x;
		myY=y;
		myXV=5;
		myYV=-5;
		myRadius=radius;
	}
	public boolean onMove() {
		int excessPartOfMove=(int).5;
		if (myY>Y_MAX-myRadius) {
			return true;
			/*excessPartOfMove=(myY-Y_MAX+myRadius)/(myYV);
			myY=Y_MAX-myRadius-(int).01;
			myX-=excessPartOfMove*myXV;
			myYV*=-1;*/
		} else if (myY<myRadius-(int).01) {
			excessPartOfMove=(myRadius-myY)/(myYV);
			myY=myRadius+(int).01;
			myX-=excessPartOfMove*myXV;
			myYV*=-1;
		}
		if (myX>X_MAX-myRadius+(int).01) {
			excessPartOfMove=(myX-X_MAX+myRadius)/(myXV);
			myX=X_MAX-myRadius-(int).01;
			myY-=excessPartOfMove*myYV;
			myXV*=-1;
		} else if (myX<myRadius-(int).01) {
			excessPartOfMove=(myRadius-myX)/(myXV);
			myX=myRadius+(int).01;
			myY-=excessPartOfMove*myYV;
			myXV*=-1;
		}
		return false;
	}
	
	
	
	public void setX(int X) {myX=X;}
	public int getX() {return myX;}
	public void setY(int Y) {myY=Y;}
	public int getY() {return myY;}
	public void setXV(int XV) {myXV=XV;}
	public int getXV() {return myXV;}
	public void setYV(int YV) {myYV=YV;}
	public int getYV() {return myYV;}
	public int getId() {return myId;}
	public int getRadius() {return myRadius;}
	public Bitmap getBitmap() {return myImage;}
	
	public boolean overlaps(Brick brick) {
		if (myX+myRadius<brick.getX())
			return false;
		if (myX-myRadius>brick.getX()+brick.getWidth())
			return false;
		if (myY+myRadius<brick.getY())
			return false;
		if (myY-myRadius>brick.getY()+brick.getWidth())
			return false;
		if ((myX>=brick.getX() && myX<=brick.getX()+brick.getWidth()) || (myY>=brick.getY() && myY<=brick.getY()+brick.getWidth())) {
			return true;
		}
		//checking corners now
		//TL corner
		if (distance(myX, myY, brick.getX(), brick.getY())<=(double) myRadius) {
			return true;
		}
		//TR corner
		if (distance(myX, myY, brick.getX()+brick.getWidth(), brick.getY())<=(double) myRadius) {
			return true;
		}
		//BL corner
		if (distance(myX, myY, brick.getX(), brick.getY()+brick.getWidth())<=(double) myRadius) {
			return true;
		}
		//BR corner
		if (distance(myX, myY, brick.getX()+brick.getWidth(), brick.getY()+brick.getWidth())<=(double) myRadius) {
			return true;
		}
		return false;
	}
	private float distance(int x1, int y1, int x2, int y2) {
		int c2 = (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
		return (float) Math.pow(((double) c2)/2, 1.0/2);
	}
	public void bounce(Brick brick) {
		// TODO Auto-generated method stub
		int prevX, prevY;
		prevX=myX-myXV;
		prevY=myY-myYV;
		boolean TL=true, TR=true, BL=true, BR=true; //corners possible to hit on both sides
		if (myXV>=0) {
			TR=false;
			BR=false;
		} else if (myXV<=0) {
			TL=false;
			BL=false;
		}
		if (myYV>=0) {
			BL=false;
			BR=false;
		} else if (myYV<=0) {
			TL=false;
			TR=false;
		}
		int sideDir=-1; //0=top, 1=right side, 2=bottom, 3=left side
		if (TL) {
			if ((double) (myXV)/(double) (brick.getX()-prevX)>=(double) (myYV)/(double) (brick.getY()-prevY)) {
				sideDir=3;
			} else {
				sideDir=0;
			}
		}
		else if (TR) {
			if ((double) (myXV)/(double) (brick.getX()+brick.getWidth()-prevX)>=(double) (myYV)/(double) (brick.getY()-prevY)) {
				sideDir=1;
			} else {
				sideDir=0;
			}
		}
		else if (BL) {
			if ((double) (myXV)/(double) (brick.getX()-prevX)>=(double) (myYV)/(double) (brick.getY()+(brick.getWidth()/5)-prevY)) {
				sideDir=3;
			} else {
				sideDir=2;
			}
		}
		else if (BR) {
			if ((double) (myXV)/(double) (brick.getX()+brick.getWidth()-prevX)>=(double) (myYV)/(double) (brick.getY()+(brick.getWidth()/5)-prevY)) {
				sideDir=1;
			} else {
				sideDir=2;
			}
		}
		if (sideDir%2==0) {
			myYV*=-1;
		} else if (sideDir%2==1) {
			myXV*=-1;
		}
	}
	public boolean checkPaddle(Paddle p) {
		if (myY+myRadius<Brick.HEIGHT*99/100) {
			return false;
		}
		if (myX>=p.getX()-p.getWidth() && myX<=p.getX()+p.getWidth()) {
			double pos= ((double) myX - (double) p.getX())/((double) p.getWidth()*2.0);
			myXV+=16*(pos);
			myYV*=-1;
			return true;
		}
		return false;
	}
}
